package com.oedtech.esl.controller.security;

import com.oedtech.esl.json.JsonResult;
import com.oedtech.esl.model.sys.Role;
import com.oedtech.esl.repository.sys.RoleDao;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

@RestController
@RequestMapping("/role_rest")
public class RoleRestController {

    @Autowired
    private RoleDao roleDao;

    @PostMapping("/add_change_role")
    public JsonResult AddOrChangeRole(@RequestBody Role role){
        if(role.getRoleCode()==null)return new JsonResult("error","角色码不能位空");

        if(role.getRoleName()==null)return new JsonResult("error","角色名不能位空");
        if(role.getId()==null){
            if(roleDao.findCountByRoleCode(role.getRoleCode())>0)return new JsonResult("error","角色已存在");
        }
        role.setModifyTime(new Date());
        roleDao.save(role);
        return new JsonResult("success","保存成功");
    }

    @RequestMapping("/delete_role")
    public void deleteRole(@RequestParam(name = "roleId")Long roleId){
        roleDao.deleteById(roleId);
    }





    //商品列表分页查询
    @RequestMapping("/listByPage")
    @ResponseBody
    public Page<Role> listByPage(int page, int size, String sortStr, boolean sortAscOrDesc, String searchObj)
    {

        Specification<Role> specification = getSpecification(searchObj);

        if (StringUtils.isEmpty(sortStr))
        {
            return roleDao.findAll(specification, PageRequest.of(page, size));
        }
        else
        {
            return roleDao.findAll(specification, PageRequest.of(page, size, new Sort((sortAscOrDesc ? Sort.Direction.ASC : Sort.Direction.DESC), sortStr)));
        }
    }
    private Specification getSpecification(String searchObj)
    {
        JSONObject search = (JSONObject) JSONValue.parse(searchObj);

        Specification<Role> specification = new Specification<Role>()
        {
            @Override
            public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder)
            {
                List<Predicate> queryList = new ArrayList<Predicate>();
                if (search.get("roleCode") != null && !StringUtils.isEmpty(search.get("roleCode") + ""))
                {
                    queryList.add(criteriaBuilder.like(root.get("roleCode"), "%"+search.get("roleCode") + "%"));
                }
                if (search.get("roleName") != null && !StringUtils.isEmpty(search.get("roleName") + ""))
                {
                    queryList.add(criteriaBuilder.like(root.get("roleName"), "%"+search.get("roleName") + "%"));
                }

                if (queryList.size() > 0)
                {
                    criteriaQuery.where(criteriaBuilder.and(queryList.toArray(new Predicate[queryList.size()])));
                }

                return criteriaQuery.getRestriction();
            }
        };

        return specification;
    }
}
